﻿using System;
using System.Collections.Generic;
using System.Xml;

namespace DarkAngels.WoW.Library.Armory.Character.tabs
{
    public struct TalentSpec
    {
        public string Description
        {
            get;
            set;
        }

        public string Spec
        {
            get;
            set;
        }

        public Glyph[] Glyphs
        {
            get;
            set;
        }

        public bool ActiveSpec
        {
            get;
            set;
        }
    }

    public struct Glyph
    {
        public string Effect
        {
            get;
            set;
        }

        public string Name
        {
            get;
            set;
        }

        public int Id
        {
            get;
            set;
        }
    }

    public class TalentsTab : AbstractDataClass
    {
        private const string XPATH_TALENTS_GROUPS =                  "/page/characterInfo/talents/talentGroup";
        private const string XPATH_CHARACTER_BASIC_INFO_NODE =       "/page/characterInfo/character";
        private Character m_char = null;
        public Character Character
        {
            get
            {
                return m_char;
            }
        }
        public TalentsTab(Character character)
        {
            m_char = character;
        }

        private TalentSpec[] m_talentree = null;
        public TalentSpec[] TalentTree
        {
            get
            {
                if (m_talentree == null)
                    LoadData();
                return m_talentree;
            }
        }

        public override void Load()
        {
            LoadData();
        }
        private void LoadData()
        {
            XmlData = Character.Provider.GetFile(BuildUri(FileName));
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(XmlData);
            XmlNodeList talentGroups = xmlDoc.SelectNodes(XPATH_TALENTS_GROUPS);
            List<String> groups = new List<string>();
            List<TalentSpec> specs = new List<TalentSpec>();
            foreach (XmlNode node in talentGroups)
            {
                TalentSpec spec = new TalentSpec();
                XmlNode talents = node.SelectSingleNode("./talentSpec");
                spec.Description = node.Attributes["prim"].Value;
                spec.Spec = talents.Attributes["value"].Value;
                if (node.Attributes["active"] != null)
                    spec.ActiveSpec = true;
                else
                    spec.ActiveSpec = false;

                XmlNodeList glyphs = node.SelectNodes("./glyphs/glyph");
                List<Glyph> glyph_list = new List<Glyph>();
                foreach (XmlNode glyph in glyphs)
                {
                    Glyph g = new Glyph();
                    g.Effect = glyph.Attributes["effect"].Value;
                    g.Id = Int32.Parse(glyph.Attributes["id"].Value);
                    g.Name = glyph.Attributes["name"].Value;
                    glyph_list.Add(g);
                }
                spec.Glyphs = glyph_list.ToArray();
                glyph_list = null;
                specs.Add(spec);
            }
            m_talentree = specs.ToArray();
            specs = null;

            if (!Character._Loaded)
            {
                XmlNode characterInfo = xmlDoc.SelectSingleNode(XPATH_CHARACTER_BASIC_INFO_NODE);
                Character._SetProperties(characterInfo);
            }
        }
        private String FileName
        {
            get
            {
                if (Character._Loaded)
                    return String.Format("character-talents.xml?{0}", Character.CharUrl);
                else
                    return String.Format("character-talents.xml?r={0}&n={1}", Character.RealmName, Character.CharacterName);
            }
        }
        private String XmlData
        {
            get;
            set;
        }
    }
}
